<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 订单管理
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\OrderValidate;

class Order extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if(request()->isPost()){
            $validate = new OrderValidate();
            $ret = $validate->scene('index')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);

            $where = [
                ['buytime','between',[date('Y-m-d'),date('Y-m-d 23:59:59')]]
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)){
                switch($fieldName){
                    case 'account'://账号
                        $userId = \app\extend\model\User::where([
                            ['account','=',$fieldValue]
                        ])->value('id');
                        $where[] = ['uid','=',$userId];
                        break;
                }
            }

            $list = [];
            $count = \app\extend\model\Order::count();
            if($count > 0){
                $list = \app\extend\model\Order::where($where)->append(['user_account_txt'])->order(['id' => 'desc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }

    /**
     * 平仓日志
     */
    public function history()
    {
        if(request()->isPost()){
            $validate = new OrderValidate();
            $ret = $validate->scene('history')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $page = input('page/d',1);
            $limit = input('limit/d',20);
            $fieldName = input('field_name','');
            $fieldValue = input('field_value','');
            $status = input('status/d','');
            $type = input('type/d','');
            $dateType = input('date_type','buytime');
            $startDate = input('start_date','');
            $endDate = input('end_date','');

            $where = [
                [$dateType,'between',[$startDate,$endDate]]
            ];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)){
                switch($fieldName){
                    case 'account'://账号
                        $userId = \app\extend\model\User::where([
                            ['account','=',$fieldValue]
                        ])->value('id');
                        $where[] = ['uid','=',$userId];
                        break;
                    case 'uid'://用户ID
                        $where[] = ['uid','=',$fieldValue];
                        break;
                }
            }

            //状态
            if (is_numeric($status)) {
                $where[] = ['status', '=', $status];
            }

            //方向
            if (is_numeric($type)) {
                $where[] = ['type', '=', $type];
            }

            $list = [];
            $count = \app\extend\model\Order::where($where)->count();
            if($count > 0){
                $list = \app\extend\model\Order::where($where)->append(['user_account_txt'])->order(['id' => 'desc'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('history');
        }
    }

    /**
     * 修改盈亏
     */
    public function editKongType()
    {
        if(request()->isPost()){
            $validate = new OrderValidate();
            $ret = $validate->scene('editKongType')->check(input());
            if(!$ret){
                return json(_E($validate->getError()));
            }

            $id = input('id/d',0);
            $kongType = input('kong_type/d',0);

            $orderInfo = \app\extend\model\Order::where([
                ['id','=',$id]
            ])->field('id,status')->find();

            if($orderInfo->status == 1){
                return json(_E('该订单已经平仓，禁止操作'));
            }

            $result = \app\extend\model\Order::where([
                ['id','=',$id],
                ['status','=',0]
            ])->update([
                'kong_type' => $kongType
            ]);

            if(!$result){
                return json(_E('修改失败'));
            } else {
                return json(_S('修改成功'));
            }
        }
    }
}
